<template>
  <div>
    <Spin :spinning="loading" tip="Loading...">
      <div style="padding-top: 1rem;"></div>
      <!-- table -->
      <FormPanel ref="formPanel" :list="formList" :cols="1" :formType="'add'" labelWidth="110" :isSubmitBtn="false" @formChange="formChangeHandle" />
      <BaseDialog :visible.sync="visibleDrawer" :title="'VIN查询'" width="850px" destroyOnClose :containerStyle="{height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px'}">
        <VinVehicle @close="closeAdd" @save="save"></VinVehicle>
      </BaseDialog>
      <BaseDialog :visible.sync="visibleAdd" :title="'配件选择'" width="850px" destroyOnClose :containerStyle="{height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px'}">
        <PeiJianSelect @close="closePart" @save="savePart"></PeiJianSelect>
      </BaseDialog>
      <div style="padding-top: 1rem;"></div>
      <div
        :style="{
          position: 'absolute',
          left: 0,
          right: 0,
          zIndex: 9,
          borderTop: '1px solid #e9e9e9',
          padding: '10px 20px',
          background: '#fff',
          textAlign: 'right'
        }"
      >
        <multiuse-button type="primary" :click="saveDialog.bind(this)">保 存</multiuse-button>
        <multiuse-button @click="closeDialog">关 闭</multiuse-button>
      </div>
    </Spin>
  </div>
</template>
<script>
  import {  editSave,quyrpNonWarranty } from '@/api/claim/factory/SpeCarNonFirstPartTypeWarrantyMaintainOil';
  import {  getPartType,getPart } from '@/api/dictionary/index';
  import VinVehicle from '@/pages/claimDemage/tools/VinVehicle.vue';
  import PeiJianSelect from '@/pages/claimDemage/tools/vehicle.vue';
  export default {
    props: ['rowData'],
    components: {
      VinVehicle,
      PeiJianSelect
    },
    data() {
      return {
        editRow: {},
        dataList1: [],
        visible: false,
        visibleDrawer: false,
        collapse: false,
        visibleAdd: false,
        infoId: null,
        flterValue: {}, //参数
        fetchParams: {}, //ipa 接口
        selectedRows: [], //选中经销商
        formList: this.createFormList1(),
        saveData: null,
        activeName: '审核记录',
        modelName:'',
        ptStatus:'',
        ownerCode:'',
        ownerParCode:'',
        appId:'',
        isDeleted: '',
        createdBy:'',
        loading: false,
        visibleAdd: false,
        part: {}
      };
    },
    async created() {
      await this.chuqi();
      await this.chuqi1();
      await this.getDetailFun(this.rowData.warrantyId);
    },
    mounted() {
    },
    methods: {
      closePart() {
        this.visibleAdd = false;
      },
      savePart(obj) {
        this.part.partCode = obj[0].partNo;
        this.part.partName = obj[0].partName;
        this.formList.find(x => x.fieldName == 'partName').initialValue = obj[0].partName;
        this.visibleAdd = false;
      },
      showDrawer() {
        this.visibleAdd = true;
      },
      async chuqi() {
        //配件类型
        // const res = await getPart();
        // if (res.resultCode == 200) {
        //   this.formList.find(x => x.fieldName == 'typeId').initialValue = '';
        //   this.formList.find(x => x.fieldName == 'typeId').itemList = res.data.map(y => ({
        //     text: y.name,
        //     value: y.code
        //   }));
        // }
        // //为form赋值
        // for (let i in this.rowData) {
        // this.formList.map(y => {
        //   if (i == y.fieldName) {
        //     y.initialValue = this.rowData[i];
        //   }
        // });}
      },
      async chuqi1() {
        //类型
        const res1 = await getPartType({type: 5592});
        if (res1.resultCode == 200) {
          this.formList.find(x => x.fieldName == 'type').initialValue = '';
          this.formList.find(x => x.fieldName == 'type').itemList = res1.data.map(y => ({
            text: y.codeCnDesc,
            value: y.codeId
          }));
        }
      },
      async getDetailFun(val) {
        this.loading = true;
        let res = await quyrpNonWarranty(val);
        this.editRow = res.data;
        for (let i in res.data) {
          this.formList.map(x => {
            if (x.fieldName == i) {
              x.initialValue = res.data[i];
            }
          });
        }
        this.loading = false;
      },
      formChangeHandle(val) {
        console.log(val);
        this.saveData = val;
      },
      // //选中
      // selectChange(rows) {
      //   this.selectedRows = rows;
      //   console.log(this.selectedRows);
      // },
      save(obj) {
        this.formList.find(x => x.fieldName == 'vin').initialValue = obj[0].vin;
        this.formList.find(x => x.fieldName == 'modelCode').initialValue = obj[0].modelCode;
        this.modelName = obj[0].modelName;
        this.visibleDrawer = false;
      },
      //保存
      chooseOrderNo() {
        this.visibleDrawer = true;
      },
      closeAdd() {
        this.visibleDrawer = false;
      },
      saveAdd() {
        this.visibleAdd = false;
        this.fetchParams = { ...this.fetchParams };
      },
      //保存
      async saveDialog() {
        this.$refs.formPanel.SUBMIT_FORM();
        if (this.saveData !== null) {

          if (this.modelName) {
            this.saveData.modelName = this.modelName;
          }

          for (let a in this.editRow) {
            if (this.saveData[a]) {
              this.editRow[a] = this.saveData[a];
            }
          }

          this.editRow.partCode = this.part.partCode || this.editRow.partCode;
          this.editRow.partName = this.part.partName || this.editRow.partName;

          this.editRow.updatedAt = null;
          this.editRow.updatedBy = null;
          this.loading = true;
          let res = await editSave(this.editRow);

          console.log(res);
          if (res.resultCode === 200) {
            this.$emit('save');
            return this.$notify.success({ title: '提示信息', message: '保存成功' });
          } else {
            this.loading = false;
            // this.$notify.error({ title: '提示信息', message: res.errMsg || '保存失败!' });
            // this.closeDialog();
          }
        }
      },
      //取消操作
      closeDialog() {
        this.$emit('close', { visible: false, data: this.selectedRows, type: 'close' });
      },
      add() {},
      createFormList1() {
        return [
          {
            type: 'INPUT',
            label: '配件代码',
            fieldName: 'partName',
            filterable: true,
            placeholder: '请选择配件...',
            readonly: true, // 只读
            rules: [{ required: true, message: '请选择配件...', trigger: 'change' }],
            unitRender: p => {
              return (
                <div>
                  <multiuse-button icon="el-icon-search" onClick={() => this.showDrawer()} />
                </div>
              );
            }
          },
          {
            type: 'INPUT',
            label: 'VIN',
            fieldName: 'vin',
            placeholder: '请选择vin...',
            rules: [{ required: true, message: '请选择vin...', trigger: 'change' }],
            initialValue: '',
            readonly: true ,// 只读
            unitRender: p => {
              return (
                <div>
                <multiuse-button icon="el-icon-search" onClick={() => this.chooseOrderNo()} />
              </div>
            );
            }
          },
          {
            type: 'INPUT',
            label: '车型代码',
            fieldName: 'modelCode',
            disabled:true,
          },
          {
            type: 'SELECT',
            label: '类型',
            fieldName: 'type',
            itemList: [],
            placeholder: '请选择类型...',
            filterable: true,
            rules: [{ required: true, message: '请选择类型...', trigger: 'change' }],
            editRequired: true
          },
          {
            type: 'TEXT_AREA',
            label: '备注',
            fieldName: 'remarks',
            placeholder: '请输入备注...',
            rows: 2,
            selfCols: 1 // 元素占据几行
          }
        ];
      }
    }
  };
</script>

<style lang="less" scoped></style>
